package com.st.bottomnavigation.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期类
 */
public class XDate {

    public static final String yyyy_MM = "yyyy-MM";

    /**
     * 简单年月格式：yyyyMM
     */
    public static final String yyyyMM = "yyyyMM";

    /**
     * 标准日期格式：yyyy-MM-dd
     */
    public static final String yyyy_MM_dd = "yyyy-MM-dd";

    /**
     * 标准时间格式：HH:mm:ss
     */
    public static final String HH_mm_ss = "HH:mm:ss";

    /**
     * 标准日期时间格式，精确到分：yyyy-MM-dd HH:mm
     */
    public static final String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";

    /**
     * 标准日期时间格式，精确到秒：yyyy-MM-dd HH:mm:ss
     */
    public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    public static final String yyyyMMdd_HHmmss = "yyyyMMdd_HHmmss";

    /**
     * 标准日期格式：yyyyMMdd
     */
    public static final String yyyyMMdd = "yyyyMMdd";

    /**
     * 标准时间格式：HHmmss
     */
    public static final String HHmmss = "HHmmss";

    /**
     * 标准日期格式：yyyyMMddHHmmss
     */
    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";


    public static String makeTime(Date time, String pattern) {
        if (time == null) return "";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(time);
    }

    public static String makeTime(long time, String pattern) {
        return makeTime(new Date(time), pattern);
    }

    public static Date makeTime(String sTime, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = df.parse(sTime);
        } catch (ParseException e) {
        }
        return date;
    }

    public static String makeTime(String pattern) {
        return makeTime(new Date(), pattern);
    }

    public static String makeYmdHms19(Date time) {
        return makeTime(time, yyyy_MM_dd_HH_mm_ss);
    }

    public static String makeYmdHms19(long time) {
        return makeTime(new Date(time), yyyy_MM_dd_HH_mm_ss);
    }

    public static Date makeYmdHms19(String sTime) {
        return makeTime(sTime, yyyy_MM_dd_HH_mm_ss);
    }

    public static String makeYmdHms16(Date time) {
        return makeTime(time, yyyy_MM_dd_HH_mm);
    }

    public static Date makeYmdHms14(String sTime) {
        return makeTime(sTime, yyyyMMddHHmmss);
    }

    public static String makeYmdHms14(long time) {
        return makeTime(time, yyyyMMddHHmmss);
    }

    public static String makeYmdHms14(Date time) {
        return makeTime(time, yyyyMMddHHmmss);
    }

    public static String getYmdHms19() {
        return makeTime(new Date(), yyyy_MM_dd_HH_mm_ss);
    }

    public static String makeYmd(Long lTime) {
        if (lTime == null) return null;
        return makeTime(lTime, yyyyMMdd);
    }

    public static String makeHms(Long lTime) {
        if (lTime == null) return null;
        return makeTime(lTime, HHmmss);
    }
    /**
     * 获取当前时间戳
     * @return
     */
    public static String getCurrentTime(){
        Date date = null;
        try {
            //获取时间戳
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            String currentTime = formatter.format(new Date());
            date = formatter.parse(currentTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String time = String.valueOf(date.getTime());
        return time;
    }

    /**
     * 时间戳转换为时间
     * @param timeStamp
     * @return
     */
    public static String timeStampToDate(long timeStamp) {
        Date date = new Date(timeStamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * String时间转换成LocalDateTime
     * @param timeString
     * @return
     */
    public static LocalDateTime StringToDate(String timeString) {
        // 定义日期时间格式
        DateTimeFormatter formatter = null;
        LocalDateTime dateTime = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // 将字符串解析为 LocalDateTime 对象
            dateTime = LocalDateTime.parse(timeString, formatter);
        }
        return dateTime;
    }

    /**
     * yyyy-MM-dd格式的日期转换yyyyMMdd
     * @param date
     * @return
     */
    public static String dateChange(String date){
        String outputFormat = "yyyyMMdd";
        String outputDate = "";
        // 将输入字符串解析为LocalDate对象
        DateTimeFormatter inputFormatter = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(date, inputFormatter);
            // 将LocalDate对象按照新的格式输出
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormat);
            outputDate = localDate.format(outputFormatter);
        }
        return outputDate;
    }
    /**
     * String时间格式化
     * @param value
     * @return
     */
    public static String dataFormat(String value) {
        if (value != null && !value.isEmpty()) {
            long timestamp = Long.parseLong(value);
            Date date = new Date(timestamp);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm");
            return dateFormat.format(date);
        } else {
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");
            return dateFormat.format(date);
        }
    }

    /**
     * 截取时间yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String extractDateTime(String inputString) {
        // 查找小数秒点的位置
        int dotIndex = inputString.indexOf('.');

        if (dotIndex != -1) {
            // 截取小数秒点前面的部分
            return inputString.substring(0, dotIndex);
        } else {
            // 如果没有小数秒点，返回原始字符串
            return inputString;
        }
    }

    /**
     * 获取当天时间-yyyy-MM-dd
     * @return
     */
    public static String getCurrentDate() {

        // 定义日期时间格式
        DateTimeFormatter formatter = null;
        String formattedDate = "";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            // 获取当前日期
            LocalDate today = LocalDate.now();

            formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 格式化日期
            formattedDate = today.format(formatter);
        }
        return formattedDate;
    }

    /**
     * 2023-12-21 23:41:31 变 23-12-21 23:41:31
     * @param originalDateTime
     * @return
     */
    public static String truncateYear(String originalDateTime) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");

        try {
            Date date = inputFormat.parse(originalDateTime);
            return outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return ""; // 或者返回一个默认值，表示转换失败
        }
    }
    public static String makeHms(Date dTime) {
        return makeTime(dTime.getTime(), HHmmss);
    }

    public static String makeHms8(long lTime) {
        return makeTime(lTime, HH_mm_ss);
    }

    public static String makeYmd(Date dTime) {
        return makeTime(dTime, yyyyMMdd);
    }

    public static String getYmd10() {
        return makeTime(new Date(), yyyy_MM_dd);
    }

    public static String getHms8() {
        return makeTime(new Date(), HH_mm_ss);
    }

    public static String getYmd_Hms_SSS() {
        return makeTime(new Date(), "yyyyMMdd_HHmmss_SSS");
    }

    public static String getYear() {
        return makeTime(new Date(), "yyyy");
    }

    public static String getMonth() {
        return makeTime(new Date(), "MM");
    }

    public static String getDay() {
        return makeTime(new Date(), "dd");
    }

    public static String getYmd() {
        return makeTime(new Date(), yyyyMMdd);
    }

    public static String getYmdHms() {
        return makeTime(new Date(), yyyyMMddHHmmss);
    }

    public static String getDay8() {
        return getYmd();
    }

    public static String getHms() {
        return makeTime(new Date(), HHmmss);
    }

    public static String dayTo10(String date) {
        return day8To10(date, "-");
    }

    public static Date getBeginTimeOfDay(String sDay8) {
        return makeTime(sDay8 + "000000", yyyyMMddHHmmss);
    }

    public static Date getEndTimeOfDay(String sDay8) {
        return makeTime(sDay8 + "235959", yyyyMMddHHmmss);
    }

    public static String day8To10(String day8, String flag) {
        if (day8 == null)
            return "";
        if (day8.length() == 4)
            return day8;
        if (day8.length() == 6)
            return day8.substring(0, 4) + flag + day8.substring(4, 6);
        if (day8.length() == 8)
            return day8.substring(0, 4) + flag + day8.substring(4, 6) + flag + day8.substring(6, 8);
        if (day8.length() == 10)
            return day8;
        return "";
    }

    public static String time14To19(String day14) {
        if (day14 == null)
            return "";
        if (day14.length() == 4)
            return day14;
        if (day14.length() == 6)
            return day14.substring(0, 4) + "-" + day14.substring(4, 6);
        if (day14.length() == 7)
            return day14;
        if (day14.length() == 8)
            return day14.substring(0, 4) + "-" + day14.substring(4, 6) + "-" + day14.substring(6, 8);
        if (day14.length() == 10)
            return day14;
        if (day14.length() == 14)
            return day14.substring(0, 4) + "-" + day14.substring(4, 6) + "-" + day14.substring(6, 8) + " " + day14.substring(8, 10) + ":" + day14.substring(10, 12) + ":" + day14.substring(12, 14);
        return "";
    }

    public static String day468ToCn(String day468) {
        if (day468 == null) {
            return "";
        } else if (day468.length() == 4) {
            return day468 + "年";
        } else if (day468.length() == 6) {
            return day468.substring(0, 4) + "年" + day468.substring(4, 6) + "月";
        } else if (day468.length() == 8) {
            return day468.substring(0, 4) + "年" + day468.substring(4, 6) + "月" + day468.substring(6, 8) + "日";
        } else {
            return "";
        }
    }

    public static String time10To8(String time10) {
        return time10To8(time10, ":");
    }

    public static String time10To8(String time10, String flag) {
        if (time10 == null)
            return "";
        if (time10.length() == 2)
            return time10;
        if (time10.length() == 4)
            return time10.substring(0, 2) + flag + time10.substring(2, 4);
        if (time10.length() == 6)
            return time10.substring(0, 2) + flag + time10.substring(2, 4) + flag + time10.substring(4, 6);
        if (time10.length() == 8)
            return time10;
        return "";
    }

    public static String addDay(String day8, int days) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat(yyyyMMdd);
        long lDate = df.parse(day8).getTime();
        return df.format(new Date(lDate + days * 24 * 3600 * 1000));
    }

    public static int diffDay(String day81, String day82) throws Exception {
        SimpleDateFormat simpledateformat = new SimpleDateFormat(yyyyMMdd);
        Date d1 = simpledateformat.parse(day81);
        Date d2 = simpledateformat.parse(day82);
        long tmp = d2.getTime() - d1.getTime();
        return (int) (tmp / (24 * 3600 * 1000));
    }

    // 1到7
    public static int getWeek(String day8) throws Exception {
        SimpleDateFormat dataFormat = new SimpleDateFormat(yyyyMMdd);
        Date date = dataFormat.parse(day8);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int posOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (posOfWeek == 0) posOfWeek = 7;
        return posOfWeek;
    }

    //1到7
    public static int getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (week == 0)
            week = 7; // 星期日是第7天
        return week;
    }

    public static String getCnWeek(int i) {
        if (1 == i)
            return "星期一";
        else if (2 == i)
            return "星期二";
        else if (3 == i)
            return "星期三";
        else if (4 == i)
            return "星期四";
        else if (5 == i)
            return "星期五";
        else if (6 == i)
            return "星期六";
        else if (7 == i)
            return "星期日";
        else
            return "星期几";
    }

    /**
     * 获取前一天的日期
     *
     * @return
     */
    public static String findTheDayBefore() throws Exception {
        return XDate.addDay(XDate.getYmd(), -1);
    }

    /**
     * 获取指定月份的天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    //获取星期一
    public static String getMonday(String day8) throws Exception {
        int week = getWeek(day8);
        return XDate.addDay(day8, -(week - 1));
    }

    //获取星期天
    public static String getSunday(String day8) throws Exception {
        int week = getWeek(day8);
        return XDate.addDay(day8, 7 - week);
    }

    public static Date addMinute(Date date, int add) {
        return new Date(date.getTime() + add * 60 * 1000);
    }

    //整秒的毫秒
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis() / 1000 * 1000;
    }

    //
    public static String diffCnHms(long lTime1, long lTime2) {
        long diff = lTime2 / 1000 - lTime1 / 1000; //相差的秒
        return diffCnHms(diff);
    }

    public static String diffCnHms(long miao) {
        if (miao < 0) return "0时0分0秒";
        long s = miao % 3600 % 60;
        long m = miao % 3600 / 60;
        long h = miao / 3600;
        return h + "时" + m + "分" + s + "秒";
    }

    public static String diffCnHm(long miao) {
        if (miao < 60) return "0分";
        String str = "";
        long m = miao % 3600 / 60;
        long h = miao / 3600;
        if (h > 0) str += (h + "时");
        if (m > 0) str += (m + "分");
        return str;
    }

    public static String diffHms(long lTime1, long lTime2) {
        long diff = lTime2 / 1000 - lTime1 / 1000; //相差的秒
        return diffHms(diff);
    }

    public static String diffHms(long miao) {
        if (miao < 0) return "00:00:00";
        long s = miao % 3600 % 60;
        String ss = s < 10 ? "0" + s : "" + s;
        long m = miao % 3600 / 60;
        String mm = m < 10 ? "0" + m : "" + m;
        long h = miao / 3600;
        String hh = h < 10 ? "0" + h : "" + h;
        return hh + ":" + mm + ":" + ss;
    }

    public static String day10To8(String day10) {
        if (day10 == null || day10.length() != 10) return day10;
        return day10.substring(0, 4) + day10.substring(5, 7) + day10.substring(8, 10);
    }

}
